package com.xy.dyson.service.impl;

import com.xy.dyson.dao.SyntheticRelationMapper;
import com.xy.dyson.entity.Resources;
import com.xy.dyson.entity.SyntheticRelation;
import com.xy.dyson.service.ResourcesService;
import com.xy.dyson.service.SyntheticRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (SyntheticRelation)表服务实现类
 *
 * @author XY
 * @since 2021-01-28 16:24:36
 */
@Service("syntheticRelationService")
public class SyntheticRelationServiceImpl implements SyntheticRelationService {
    @Resource
    private SyntheticRelationMapper syntheticRelationMapper;

    @Autowired
    ResourcesService resourcesService;

    /**
     * 新增或修改
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public void saveOrUpdate(SyntheticRelation syntheticRelation) {

    }




    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SyntheticRelation queryById(Integer id) {
        return this.syntheticRelationMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<SyntheticRelation> queryAllByLimit(int offset, int limit) {
        return this.syntheticRelationMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param syntheticRelation 实例对象
     * @return 实例对象
     */
    @Override
    public SyntheticRelation insert(SyntheticRelation syntheticRelation) {
        this.syntheticRelationMapper.insert(syntheticRelation);
        return syntheticRelation;
    }

    /**
     * 修改数据
     *
     * @param syntheticRelation 实例对象
     * @return 实例对象
     */
    @Override
    public SyntheticRelation update(SyntheticRelation syntheticRelation) {
        this.syntheticRelationMapper.update(syntheticRelation);
        return this.queryById(syntheticRelation.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.syntheticRelationMapper.deleteById(id) > 0;
    }

    @Override
    public List<SyntheticRelation> queryByResourceId(Integer id) {
        SyntheticRelation syntheticRelation = new SyntheticRelation();
        syntheticRelation.setResourceId(id);
        List<SyntheticRelation> syntheticRelations = syntheticRelationMapper.queryAll(syntheticRelation);
        if(syntheticRelations!=null&&syntheticRelations.size()>0){
            List<Resources> collect = syntheticRelations.stream().map(item -> {
                Resources resources = resourcesService.queryById(item.getResourceId());
                return resources;
            }).collect(Collectors.toList());

        }
        return syntheticRelations;
    }

    @Override
    public List<SyntheticRelation> queryAll(SyntheticRelation syntheticRelation) {

        return syntheticRelationMapper.queryAll(syntheticRelation);
    }

    @Override
    public void insertBatch(List<SyntheticRelation> collect) {
        syntheticRelationMapper.insertBatch(collect);
    }

    @Override
    public void deleteByResourceId(Integer resourceId) {
        syntheticRelationMapper.deleteByResourceId(resourceId);
    }

}